package com.seis_645.appTesting;

import android.database.sqlite.SQLiteDatabase;
import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;

public class CarDataSource {

	// database fields
	private SQLiteDatabase database;
	private MySQLiteHelper db_helper;
	private String[] all_columns = { MySQLiteHelper.CAR_ID,
			MySQLiteHelper.CAR_YEAR, MySQLiteHelper.CAR_MAKE,
			MySQLiteHelper.CAR_MODEL, MySQLiteHelper.CAR_PKAGE,
			MySQLiteHelper.CAR_GMHWY, MySQLiteHelper.CAR_GMCITY,
			MySQLiteHelper.CAR_PRICE, MySQLiteHelper.CAR_DRIVEABILITY,
			MySQLiteHelper.CAR_COMFORT, MySQLiteHelper.CAR_APPEARANCE,
			MySQLiteHelper.CAR_SAFETY, MySQLiteHelper.CAR_ACCESSORIES };

	public CarDataSource(Context context) {
		this.db_helper = new MySQLiteHelper(context);
	}

	public void open() throws SQLException {
		database = db_helper.getWritableDatabase();
	}

	public void close() {
		db_helper.close();
	}

	public Car create_car(String make, String model, int year, String pkage,
			int gmHwy, int gmCity, int price, int driveability, int comfort,
			int appearance, int safety, int accessories) {
		ContentValues values = new ContentValues();
		values.put(MySQLiteHelper.CAR_MAKE, make);
		values.put(MySQLiteHelper.CAR_YEAR, year);
		values.put(MySQLiteHelper.CAR_MODEL, model);
		values.put(MySQLiteHelper.CAR_PKAGE, pkage);
		values.put(MySQLiteHelper.CAR_GMHWY, gmHwy);
		values.put(MySQLiteHelper.CAR_GMCITY, gmCity);
		values.put(MySQLiteHelper.CAR_PRICE, price);
		values.put(MySQLiteHelper.CAR_DRIVEABILITY, driveability);
		values.put(MySQLiteHelper.CAR_COMFORT, comfort);
		values.put(MySQLiteHelper.CAR_APPEARANCE, appearance);
		values.put(MySQLiteHelper.CAR_SAFETY, safety);
		values.put(MySQLiteHelper.CAR_ACCESSORIES, accessories);

		long insert_id = this.database.insert(MySQLiteHelper.TBL_CAR, null,
				values);

		Cursor cursor = this.database.query(MySQLiteHelper.TBL_CAR,
				this.all_columns, MySQLiteHelper.CAR_ID + " = " + insert_id,
				null, null, null, null);
		cursor.moveToFirst();
		Car new_car = cursor_to_car(cursor);
		cursor.close();

		return new_car;
	}

	public void delete_car(Car car) {
		long id = car.get_id();
		System.out.println("Comment deleted with id: " + id);
		this.database.delete(MySQLiteHelper.TBL_CAR, MySQLiteHelper.CAR_ID
				+ " = " + id, null);
	}

	public Car get_car(long id) {

		Cursor cursor = this.database.query(MySQLiteHelper.TBL_CAR,
				all_columns, MySQLiteHelper.CAR_ID + " = " + id, null, null,
				null, null);
		cursor.moveToFirst();
		Car new_car = cursor_to_car(cursor);

		return new_car;
	}

	public void update_car(long id, String make, String model, int year, String pkage,
			int gmHwy, int gmCity, int price, int driveability, int comfort,
			int appearance, int safety, int accessories) {
		ContentValues values = new ContentValues();
		values.put(MySQLiteHelper.CAR_MAKE, make);
		values.put(MySQLiteHelper.CAR_YEAR, year);
		values.put(MySQLiteHelper.CAR_MODEL, model);
		values.put(MySQLiteHelper.CAR_PKAGE, pkage);
		values.put(MySQLiteHelper.CAR_GMHWY, gmHwy);
		values.put(MySQLiteHelper.CAR_GMCITY, gmCity);
		values.put(MySQLiteHelper.CAR_PRICE, price);
		values.put(MySQLiteHelper.CAR_DRIVEABILITY, driveability);
		values.put(MySQLiteHelper.CAR_COMFORT, comfort);
		values.put(MySQLiteHelper.CAR_APPEARANCE, appearance);
		values.put(MySQLiteHelper.CAR_SAFETY, safety);
		values.put(MySQLiteHelper.CAR_ACCESSORIES, accessories);
		this.database.update(MySQLiteHelper.TBL_CAR, values,
				MySQLiteHelper.CAR_ID + " = " + id, null);
		System.out.println("updated car with id: " + id);
	}

	public List<Car> get_all_cars() {
		List<Car> cars = new ArrayList<Car>();

		Cursor cursor = this.database.query(MySQLiteHelper.TBL_CAR,
				all_columns, null, null, null, null, null);
		cursor.moveToFirst();
		while (!cursor.isAfterLast()) {
			Car car = cursor_to_car(cursor);
			cars.add(car);
			cursor.moveToNext();
		}

		cursor.close();
		return cars;
	}

	private Car cursor_to_car(Cursor cursor) {
		Car car = new Car(cursor.getString(2), cursor.getString(3),
				cursor.getInt(1), cursor.getString(4), cursor.getInt(5),
				cursor.getInt(6), cursor.getInt(7), cursor.getInt(8),
				cursor.getInt(9), cursor.getInt(10), cursor.getInt(11),
				cursor.getInt(12));
		car.set_id(cursor.getLong(0));
		return car;
	}

}
